package com.itheima.config;


import com.itheima.factory.BeanFactory;
import com.itheima.service.AdminService;
import com.itheima.service.impl.AdminServiceImpl;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

public class JdbcConfig {

    @Value(value = "${jdbc.driver}")
    private String driver;

    @Value(value = "${jdbc.url}")
    private String url;

    @Value(value = "${jdbc.user}")
    private String user;

    @Value(value = "${jdbc.password}")
    private String password;

//    @Autowired
//    private BeanFactory beanFactory;


    @Bean(name = "queryRunner")
    @Scope(value = "prototype")
    public QueryRunner createQueryRunner(@Qualifier(value = "dataSource1") DataSource dataSource) {
        return new QueryRunner(dataSource);
    }


    @Bean(name = "dataSource1")
    public DataSource createDataSource1() throws PropertyVetoException {
        ComboPooledDataSource ds = new ComboPooledDataSource();
        ds.setDriverClass(driver);
        ds.setJdbcUrl(url);
        ds.setUser(user);
        ds.setPassword(password);
        return ds;
    }

    @Bean(name = "dataSource2")
    public DataSource createDataSource2() throws PropertyVetoException {
        ComboPooledDataSource ds = new ComboPooledDataSource();
        ds.setDriverClass(driver);
        ds.setJdbcUrl(url);
        ds.setUser(user);
        ds.setPassword(password);
        return ds;
    }


//    @Bean(name = "adminServiceProxy")
//    public AdminServiceImpl createAdminServiceProxy() {
//        return beanFactory.getAdminService();
//    }
}
